package first.codeTop.tencent;

/**
 * @ClassName LC69
 * @description:
 * @author: 荣燊
 * @create: 2021-10-25 16:31
 **/
public class LC69 {
    // Sqrt(x)
    public static void main(String[] args) {
        System.out.println(mySqrt(8));
    }

    // 二分法
    public static int mySqrt(int x) {
        int left = 0;
        int right = x;
        int ans = -1;
        while (left <= right) {
            int mid = (left+right) / 2;
            if ((long)mid * mid <= x) {
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return ans;
    }
}
